package net.maku.biz.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.biz.convert.BizLegalAidConvert;
import net.maku.biz.entity.BizLegalAidEntity;
import net.maku.biz.service.BizLegalAidService;
import net.maku.biz.query.BizLegalAidQuery;
import net.maku.biz.vo.BizLegalAidVO;
import net.maku.member.entity.MemberUserEntity;
import net.maku.member.service.MemberUserService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
* 法律援助
*
* @author 阿沐 babamu@126.com
* @since 1.0.0 2025-03-30
*/
@RestController
@RequestMapping("biz/biz_legal_aid")
@Tag(name="法律援助")
@AllArgsConstructor
public class BizLegalAidController {
    private final BizLegalAidService bizLegalAidService;
    private final MemberUserService memberUserService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('biz:biz_legal_aid:page')")
    public Result<PageResult<BizLegalAidVO>> page(@ParameterObject @Valid BizLegalAidQuery query){
        PageResult<BizLegalAidVO> page = bizLegalAidService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('biz:biz_legal_aid:info')")
    public Result<BizLegalAidVO> get(@PathVariable("id") Long id){
        BizLegalAidEntity entity = bizLegalAidService.getById(id);
        BizLegalAidVO legalAidVO = BizLegalAidConvert.INSTANCE.convert(entity);
        MemberUserEntity memberUser = memberUserService.getById(entity.getMemberId());
        legalAidVO.setMemberName(memberUser.getRealName());
        return Result.ok(BizLegalAidConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('biz:biz_legal_aid:save')")
    public Result<String> save(@RequestBody BizLegalAidVO vo){
        bizLegalAidService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('biz:biz_legal_aid:update')")
    public Result<String> update(@RequestBody @Valid BizLegalAidVO vo){
        bizLegalAidService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('biz:biz_legal_aid:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        bizLegalAidService.delete(idList);

        return Result.ok();
    }
}